package cn.com.headfree.feedback.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gta.edu.sdk.common.dto.Page;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TFeedBack;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.feedback.service.IFeedbackService;

/**  
 * @Title  FeedBackController.java   
 * @Package cn.com.headfree.feedback.controller   
 * @Description 意见反馈controller
 * @author yangwang 
 * @date  2020年8月3日 下午2:45:29   
 */
@Controller
@RequestMapping(value = "/feedback", produces = "application/json;charset=UTF-8")
public class FeedbackController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(FeedbackController.class);

	@Autowired
	private IFeedbackService feedbackService;
	/**
	 * 配置环境
	 *  */
	@Value("${spring.profiles.active}")  
	private String spring_profiles_active;
	
	/**
	 * 保存意见反馈
	 * @param request
	 * @param feeback 意见信息
	 * @return ResultData
	 *  */
	@RequestMapping("/saveFeedback")
    @ResponseBody
    public ResultData saveFeedback(HttpServletRequest request,@RequestBody TFeedBack feeback) {
		ResultData result = new ResultData();
		try {
			//获取用户登录信息
			User user = this.getLoginUserInfo(request);
			//测试数据--start
			if(StringUtils.equals(spring_profiles_active, "dev")) {
				user = new User();
				user.setId("F9D687B69020000EE0430A8A018B000E");
				user.setName("朱玉良");
				user.setUserName("402018");
				user.setBaseOrgId("A21258CA31F24DF7955491D3CE218255");
				user.setOrgAnisationName("营销业务系统");
				user.setFax("402018");
			}
	        //测试数据--end
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				feedbackService.saveFeedback(user, feeback);
				LOG.info("保存意见反馈信息成功");
			}else {
				result.setErrorMsg("获取用户登录信息失败");
	            result.setSuccess(false);
	            LOG.error("获取用户登录信息失败");
			}
        } catch (Exception e) {
            result.setErrorMsg("保存意见反馈信息异常");
            result.setSuccess(false);
            LOG.error("保存意见反馈信息异常", e);
        }
        return result;
	}
	
	/**
	 * 删除意见反馈
	 * @param request
	 * @param ids 多个id以逗号分隔
	 * @return ResultData
	 *  */
	@RequestMapping("/deleteFeedback")
    @ResponseBody
    public ResultData deleteFeedback(HttpServletRequest request,String ids) {
		ResultData result = new ResultData();
		try {
			//获取用户登录信息
			User user = this.getLoginUserInfo(request);
			//测试数据--start
			if(StringUtils.equals(spring_profiles_active, "dev")) {
				user = new User();
				user.setId("F9D687B69020000EE0430A8A018B000E");
				user.setName("朱玉良");
				user.setUserName("402018");
				user.setBaseOrgId("A21258CA31F24DF7955491D3CE218255");
				user.setOrgAnisationName("营销业务系统");
				user.setFax("402018");
			}
	        //测试数据--end
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				feedbackService.deleteFeedback(user, ids);
				LOG.info("删除意见反馈信息成功");
			}else {
				result.setErrorMsg("获取用户登录信息失败");
	            result.setSuccess(false);
	            LOG.error("获取用户登录信息失败");
			}
        } catch (Exception e) {
            result.setErrorMsg("删除意见反馈信息异常");
            result.setSuccess(false);
            LOG.error("删除意见反馈信息异常", e);
        }
        return result;
	}
	
	/**
	 * 获取意见反馈
	 * @param id 主键
	 * @return ResultData
	 *  */
	@RequestMapping("/getFeedback")
    @ResponseBody
    public ResultData getFeedback(String id) {
		ResultData result = new ResultData();
		try {
			result.setData(feedbackService.getFeedback(id));
			LOG.info("获取意见反馈信息成功");
        } catch (Exception e) {
            result.setErrorMsg("获取意见反馈信息异常");
            result.setSuccess(false);
            LOG.error("获取意见反馈信息异常", e);
        }
        return result;
	}
	
	/**
	 * 获取意见反馈分页信息
	 * @return ResultData
	 * @param page 分页
	 * @param feeback 
	 *  */
	@RequestMapping("/getFeedbackPage")
    @ResponseBody
    public ResultData getFeedbackPage(HttpServletRequest request,Page<TFeedBack> page,@RequestBody TFeedBack feeback) {
		ResultData result = new ResultData();
		try {
			//获取用户登录信息
			User user = this.getLoginUserInfo(request);
			//测试数据--start
			if(StringUtils.equals(spring_profiles_active, "dev")) {
				user = new User();
				user.setId("F9D687B69020000EE0430A8A018B000E");
				user.setName("朱玉良");
				user.setUserName("402018");
				user.setBaseOrgId("A21258CA31F24DF7955491D3CE218255");
				user.setOrgAnisationName("营销业务系统");
				user.setFax("402018");
			}
	        //测试数据--end
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				feedbackService.getPage(user, page, feeback);
				result.setData(page);
				LOG.info("获取意见反馈信息成功");
			}else {
				result.setErrorMsg("获取用户登录信息失败");
	            result.setSuccess(false);
	            LOG.error("获取用户登录信息失败");
			}
        } catch (Exception e) {
            result.setErrorMsg("获取意见反馈信息异常");
            result.setSuccess(false);
            LOG.error("获取意见反馈信息异常", e);
        }
        return result;
	}
	
	/**
	 * 获取意见反馈分页信息
	 * @return ResultData
	 * @param page 分页
	 * @param feeback 
	 *  */
	@RequestMapping("/getUserFeedbackPage")
    @ResponseBody
    public ResultData getUserFeedbackPage(HttpServletRequest request,Page<TFeedBack> page,@RequestBody TFeedBack feeback) {
		ResultData result = new ResultData();
		try {
			//获取用户登录信息
			User user = this.getLoginUserInfo(request);
			//测试数据--start
			if(StringUtils.equals(spring_profiles_active, "dev")) {
				user = new User();
				user.setId("F9D687B69020000EE0430A8A018B000E");
				user.setName("朱玉良");
				user.setUserName("402018");
				user.setBaseOrgId("A21258CA31F24DF7955491D3CE218255");
				user.setOrgAnisationName("营销业务系统");
				user.setFax("402018");
			}
	        //测试数据--end
			if((null != user)&&(StringUtils.isNotBlank(user.getId()))) {
				feedbackService.getPage(user, page, feeback);
				result.setData(page);
				LOG.info("获取意见反馈信息成功");
			}else {
				result.setErrorMsg("获取用户登录信息失败");
	            result.setSuccess(false);
	            LOG.error("获取用户登录信息失败");
			}
        } catch (Exception e) {
            result.setErrorMsg("获取意见反馈信息异常");
            result.setSuccess(false);
            LOG.error("获取意见反馈信息异常", e);
        }
        return result;
	}
	
	/**
	 * 获取意见反馈状态数据字典数据
	 * @return ResultData
	 *  */
	@RequestMapping("/getFeedbackStatus")
    @ResponseBody
    public ResultData  getFeedbackStatus() {
		ResultData result = new ResultData();
		try {
			result.setData(feedbackService.getDictionarys("feedback_statu"));
			LOG.info("获取意见反馈数据字典数据成功");
        } catch (Exception e) {
            result.setErrorMsg("获取意见反馈数据字典数据异常");
            result.setSuccess(false);
            LOG.error("获取意见反馈数据字典数据异常", e);
        }
        return result;
	}
	
}
